package com.knowledgegraph.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.knowledgegraph.entity.EntityType;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 实体类型Mapper接口
 * 
 * @author system
 * @since 2024-01-01
 */
@Mapper
public interface EntityTypeMapper extends BaseMapper<EntityType> {

    /**
     * 获取所有启用的实体类型，按排序序号排序
     */
    @Select("SELECT * FROM entity_types WHERE enabled = 1 AND deleted = 0 ORDER BY sort_order ASC, create_time ASC")
    List<EntityType> selectEnabledTypes();

    /**
     * 获取用户所有启用的实体类型，按排序序号排序
     */
    @Select("SELECT * FROM entity_types WHERE enabled = 1 AND deleted = 0 AND creator_id = #{currentUserId} ORDER BY sort_order ASC, create_time ASC")
    List<EntityType> selectUserEntityTypes(Long currentUserId);

    /**
     * 根据类型代码查询实体类型
     */
    @Select("SELECT * FROM entity_types WHERE type_code = #{typeCode} AND deleted = 0")
    EntityType selectByTypeCode(String typeCode);

    /**
     * 检查类型代码是否已存在
     */
    @Select("SELECT COUNT(*) FROM entity_types WHERE type_code = #{typeCode} AND deleted = 0 AND id != #{excludeId}")
    int countByTypeCode(String typeCode, Long excludeId);
}